//- Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
//- 
//- Licensed under the Apache License, Version 2.0 (the "License");
//- you may not use this file except in compliance with the License.
//- You may obtain a copy of the License at
//- 
//-     http://www.apache.org/licenses/LICENSE-2.0
//- 
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.

extends layout

block content
  if user
    h1.profile-username #{user.username}'s Profile
    .profile-coin 💰 Coins: #{user.coin}
    if isCurrent
      button.recharge-button Recharge 1000 Coins
      .referral-code-container
        .referral-code= user.referral_code
        .referral-rule When a new user registers through your referral code, you will earn $888, and an additional $1888 when they pay for their first order.
  else 
    h1 User not found

  link(rel="stylesheet", href="/css/profile.css")
  script.
    const rechargeButton = document.querySelector('.recharge-button')
    if (rechargeButton) {
      rechargeButton.addEventListener('click', async () => {
        const res = await fetch('/api/recharge', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          }
        })

        const data = await res.json()
        if (data.success) {
          window.location.reload()
        }
      })
    }